﻿namespace WMS6.Web.UserControls
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using WMS6.Web.CommonMethodServiceRef;

    public class MultipleSelectControl : UserControl
    {
        public DropDownList ddlTempDropDownList = new DropDownList();
        public string DefaultSelected = string.Empty;
        protected ListBox MultipleListBox;
        public List<SysCode4DDL> sysCode4DDLList = new List<SysCode4DDL>();

        private void BindItems()
        {
            int i;
            if ((this.sysCode4DDLList != null) && (this.sysCode4DDLList.Count > 0))
            {
                List<string> defaultSelectedList = null;
                if (!string.IsNullOrEmpty(this.DefaultSelected))
                {
                    defaultSelectedList = new List<string>();
                    defaultSelectedList = this.DefaultSelected.Split(new char[] { ',' }).ToList<string>();
                }
                for (i = 0; i < this.sysCode4DDLList.Count; i++)
                {
                    System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem(this.sysCode4DDLList[i].SysCodeText, this.sysCode4DDLList[i].SysCodeValue);
                    if ((defaultSelectedList != null) && defaultSelectedList.Contains(item.Value))
                    {
                        item.Selected = true;
                    }
                    this.MultipleListBox.Items.Add(item);
                }
            }
            else if ((this.ddlTempDropDownList != null) && (this.ddlTempDropDownList.Items.Count > 0))
            {
                for (i = 0; i < this.ddlTempDropDownList.Items.Count; i++)
                {
                    this.MultipleListBox.Items.Add(this.ddlTempDropDownList.Items[i]);
                }
            }
        }

        private string GetSelectItem()
        {
            string strSelect = "";
            for (int i = 0; i < this.MultipleListBox.Items.Count; i++)
            {
                if (this.MultipleListBox.Items[i].Selected)
                {
                    strSelect = strSelect + this.MultipleListBox.Items[i].Value + ",";
                }
            }
            if (strSelect != "")
            {
                strSelect = strSelect.Substring(0, strSelect.LastIndexOf(","));
            }
            return strSelect;
        }

        private string GetSelectItemText()
        {
            string strSelect = "";
            for (int i = 0; i < this.MultipleListBox.Items.Count; i++)
            {
                if (this.MultipleListBox.Items[i].Selected)
                {
                    strSelect = strSelect + this.MultipleListBox.Items[i].Text + ",";
                }
            }
            if (strSelect != "")
            {
                strSelect = strSelect.Substring(0, strSelect.LastIndexOf(","));
            }
            return strSelect;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.BindItems();
            }
            ScriptManager.RegisterClientScriptBlock(this, base.GetType(), "ReRegeMultipleSelectControl" + this.MultipleListBox.ClientID, "$(document).ready(function() {  $('#" + this.MultipleListBox.ClientID + "').multiSelect({  noneSelectedText: '', checkAllText: '全选', unCheckAllText: '取消', selectedList: 100 , minWidth: 150, fadeSpeed: 300,  maxHeight: 180 });}); ", true);
        }

        public string ClientID
        {
            get
            {
                return this.MultipleListBox.ClientID;
            }
        }

        public DropDownList ListItem
        {
            get
            {
                return this.ddlTempDropDownList;
            }
        }

        public List<SysCode4DDL> ListItemSource
        {
            get
            {
                return this.sysCode4DDLList;
            }
            set
            {
                this.sysCode4DDLList = value;
            }
        }

        public string ReturnText
        {
            get
            {
                return this.GetSelectItemText();
            }
        }

        public string ReturnValue
        {
            get
            {
                return this.GetSelectItem();
            }
        }
    }
}

